package priv.conceit.sc.hoxton.common.web.exception;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import priv.conceit.sc.hoxton.common.web.WebResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;


/**
 * sc-hoxton
 *
 * @author Conceit
 * 2020/6/23 增加方法
 * @since 1.0.0, 2020/6/23
 */
@RestControllerAdvice(basePackages = {"priv.conceit"})
@Slf4j
public class WebExceptionHandle extends RuntimeException {


	/**
	 * 处理自定义的异常
	 *
	 * @param e WisdomteamException
	 * @return WebResponse
	 * @author conceit
	 */
	@ExceptionHandler(Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public WebResponse publicServiceException(HttpServletRequest request, HttpServletResponse response, Exception e) {
		log.error(e.getMessage(), e);
		WebExceptionEnum psee = WebExceptionEnum.EX_SYSTEM_ERROR;
		return WebResponse.build(psee.getKey(), psee.getValue());
	}

	/**
	 * 处理所有不可知的异常
	 *
	 * @param e RuntimeException
	 * @return WebResponse
	 * @author conceit
	 */
	@ExceptionHandler(RuntimeException.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public WebResponse otherRuntimeExceptionHandler(HttpServletRequest request, HttpServletResponse response,Exception e) {
		log.error(e.getMessage(), e);
		WebExceptionEnum psee = WebExceptionEnum.EX_SYSTEM_ERROR;
		return WebResponse.build(psee.getKey(), psee.getValue());
	}

	/**
	 * 处理自定义的异常
	 *
	 * @param e WebException
	 * @return WebResponse
	 * @author conceit
	 */
	@ExceptionHandler(WebException.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public WebResponse publicServiceException(HttpServletRequest request, HttpServletResponse response, WebException e) {
		log.error(e.getMessage(), e);
		WebExceptionEnum psee = WebExceptionEnum.valueOf(e.getMessage());
		return WebResponse.build(psee.getKey(), psee.getValue());
	}


	/**
	 * 处理MethodArgumentNotValidException异常
	 *
	 * @param e MethodArgumentNotValidException
	 * @return WebResponse
	 * @author conceit
	 */
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public WebResponse methodArgumentNotValidException(HttpServletRequest request, HttpServletResponse response,MethodArgumentNotValidException e) {
		FieldError fieldError = e.getBindingResult().getFieldErrors().get(0);
		Object[] args = fieldError.getArguments();
		String defaultMessage=fieldError.getDefaultMessage();
		String field=fieldError.getField();
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(field).append("-").append(defaultMessage);
		String msg = "请求参数不合法";
		log.error(msg);
		log.error(stringBuffer.toString());
		return WebResponse.build(HttpStatus.BAD_REQUEST.value(), stringBuffer.toString());
	}

	/**
	 * constraintViolationException  处理@Validated注解对 形参的过异常
	 * @param request
	 * @param response
	 * @param e
	 * @return: com.guohua.wisdomteam.common.util.RestfulResponse
	 * @author: conceit
	 * @date: 2019/10/11 19:20
	 */
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(ConstraintViolationException.class)
	public WebResponse constraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e){
		Set<ConstraintViolation<?>> constraintViolationSet=e.getConstraintViolations();
		ConstraintViolation constraintViolation=constraintViolationSet.stream().findFirst().get();
		PathImpl path= (PathImpl) constraintViolation.getPropertyPath();
		StringBuffer stringBuffer=new StringBuffer();
		stringBuffer.append(path.getLeafNode().asString());
		stringBuffer.append(constraintViolation.getMessage());
		String msg = "请求参数不合法";
		log.error(msg);
		log.error(stringBuffer.toString());
		return WebResponse.build(HttpStatus.BAD_REQUEST.value(), stringBuffer.toString());
	}

	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(MissingServletRequestParameterException.class)
	public WebResponse missingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,MissingServletRequestParameterException e){
		String msg = "请求参数不合法";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(e.getParameterName());

		log.error(msg);
		log.error(stringBuffer.toString());
		return  WebResponse.build(HttpStatus.BAD_REQUEST.value(), stringBuffer.toString());

	}

}
